package io.renren.controller;

import io.renren.annotation.Login;
import io.renren.annotation.LoginUser;
import io.renren.common.utils.ConfigConstant;
import io.renren.common.utils.DateUtils;
import io.renren.common.utils.DecimalUtils;
import io.renren.common.utils.R;
import io.renren.entity.MemberEntity;
import io.renren.entity.PriceBookEntity;
import io.renren.service.PriceBookService;
import io.renren.service.SysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import springfox.documentation.annotations.ApiIgnore;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

/**
 * 订单接口
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-03-23 15:31
 */
@RestController
@RequestMapping("/api/pricebook")
@Api(tags = "市场接口")
public class ApiPriceBookController extends AbstractController {

	@Autowired
	PriceBookService priceBookService;

	@Autowired
	SysConfigService sysConfigService;

	@Login
	@PostMapping("marketInfo")
	@ApiOperation("市场信息")
	public R marketInfo(@ApiIgnore @LoginUser MemberEntity member) {
		PriceBookEntity priceBook = priceBookService.selectOne(new EntityWrapper<PriceBookEntity>().orderBy("create_time", false).last("limit 0,1"));
		if (priceBook != null) {
			priceBook.setVol(priceBookService.totalVolume(DateUtils.getStartOfDay(new Date())));
		} else {
			double maxPrice = sysConfigService.getValueD(ConfigConstant.ORDER_MAX_PRICE);
			double rise = sysConfigService.getValueD(ConfigConstant.PRICE_UP_RISE);
			priceBook = new PriceBookEntity();
			priceBook.setClosePrice(maxPrice);
			priceBook.setOpenPrice(maxPrice);
			priceBook.setLowPrice(maxPrice);
			priceBook.setHighPrice(maxPrice);
			priceBook.setVol(11L);
			priceBook.setRise(rise);
		}
		// 美元计价
		double usdcny = sysConfigService.getValueD(ConfigConstant.USD_CNY);
		PriceBookEntity priceBookUSD = new PriceBookEntity();
		priceBookUSD.setClosePrice(DecimalUtils.div(priceBook.getClosePrice(), usdcny, 4));
		priceBookUSD.setOpenPrice(DecimalUtils.div(priceBook.getOpenPrice(), usdcny, 4));
		priceBookUSD.setLowPrice(DecimalUtils.div(priceBook.getLowPrice(), usdcny, 4));
		priceBookUSD.setHighPrice(DecimalUtils.div(priceBook.getHighPrice(), usdcny, 4));
		priceBookUSD.setVol(priceBook.getVol());
		priceBookUSD.setRise(priceBook.getRise());
		// 总资产
		double asset = DecimalUtils.round(DecimalUtils.mul(priceBook.getClosePrice(), DecimalUtils.add(member.getBalance(), member.getBlockedBalance())), 2);
		return R.ok().put("priceBook", priceBook).put("priceBookUSD", priceBookUSD).put("asset", asset);
	}

	@Login
	@PostMapping("priceChart")
	@ApiOperation("价格图表")
	public R priceChart() {
		List<PriceBookEntity> list = priceBookService.selectList(new EntityWrapper<PriceBookEntity>().setSqlSelect("close_price", "vol", "create_time")
				.orderBy("create_time", false).last("limit 0,100"));
		if (CollectionUtils.isNotEmpty(list)) {
			for (PriceBookEntity priceBookEntity : list) {
				priceBookEntity.setFormatTime(priceBookEntity.getCreateTime().getTime());
			}
			Collections.reverse(list);
		}
		return R.ok().put("list", list);
	}
}
